import { request } from '@/service/request';
import type {
  DeptVO,
  System,
  UserChangeStatus,
  UserInfoResult,
  UserInfoVO,
  UserPassword,
  UserSearchParams,
  UserVO
} from '@/service/types/system';

export function getPageList(params?: UserSearchParams) {
  return request<System.UserVo>({
    url: '/system/user/list',
    method: 'get',
    params
  });
}
/** 查询部门下拉树结构 */
export const deptTreeSelect = () => {
  return request<DeptVO>({
    url: '/system/user/deptTree',
    method: 'get'
  });
};
/** 修改状态 */
export const changeStatus = (params: UserChangeStatus) => {
  return request({
    url: '/system/user/changeStatus',
    method: 'PUT',
    data: params
  });
};
/** 重置密码 */
export const resetPassword = (type: string, params: UserPassword) => {
  return request({
    url: `/system/user/resetPwd/${type}`,
    method: 'PUT',
    data: params
  });
};

/** 获取当前用户信息 */
export const getUserInfo = (userId: string) => {
  return request<UserInfoVO>({
    url: `/system/user/${userId}`,
    method: 'GET'
  });
};

/** 新增用户 */
export const addUser = (data: UserVO) => {
  return request({
    url: '/system/user',
    method: 'post',
    data
  });
};
// 编辑
export const editUser = (data: UserVO) => {
  return request({
    url: '/system/user',
    method: 'PUT',
    data
  });
};
// 删除用户
export const delUser = (ids: number[]) => {
  return request({
    url: `/system/user/${ids}`,
    method: 'DELETE'
  });
};

// 解析数据
export const parseExcel = (formData: any) => {
  return request({
    url: '/system/user/parse',
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: formData
  });
};
// 导入数据
export const importData = (data: UserVO[], updateSupport: string) => {
  return request<API.ImportResult>({
    url: `/system/user/import/${updateSupport}`,
    method: 'POST',
    data
  });
};
// 获取用户列表
export const getAllUsers = (params?: UserSearchParams) => {
  return request<UserVO>({
    url: '/system/user/all',
    method: 'get',
    params
  });
};

export const profile = () => {
  return request<UserInfoResult>({
    url: '/system/user/profile',
    method: 'get'
  });
};

export const updateUserProfile = (data: UserVO) => {
  return request({
    url: '/system/user/profile',
    method: 'put',
    data
  });
};

/**
 * 用户密码重置
 *
 * @param oldPassword 旧密码
 * @param newPassword 新密码
 */
export const updateUserPwd = (oldPassword: string, newPassword: string) => {
  const data = {
    oldPassword,
    newPassword
  };
  return request({
    url: '/system/user/profile/updatePwd',
    method: 'put',
    headers: {
      isEncrypt: true,
      repeatSubmit: false
    },
    data
  });
};
/**
 * 用户头像上传
 *
 * @param data 头像文件
 */
export const uploadAvatar = (data: FormData) => {
  return request({
    url: '/system/user/profile/avatar',
    method: 'post',
    data
  });
};
